// Own XML parser, made for ClearBass 2.3
// Parser version: 1.1
// Dedicated for this kind of 'syntax':
// <NAME1 param1="value" param2="value" param3="value" [etc...]/>
// Comparing to older version, parsing is much faster
// (c) 2010 mc_kibel


//FAST parsing, it may cause problems if you change order of elements in .xml!
#define FAST 

//PRECISE parsing, no problems when you change order of elements in .xml. It's better, but slower.
//#define PRECISE

#define fseek lseek

int fgetc (int file)
{
  char tmp;
  fread(file,&tmp,1);
  return (tmp);
}

int position_counter = 0; //Position in opened file

int XML_FindParamsInTree (int stream, char *tree, int pcount, char params[][16], char dest[][10])
{
    int result;
    int i;
    int fsize;
    int to_search_len;
    char to_search[128] = {0,};
    char read[128] = {0,};
    char tmp[128] = {0,};
    fsize = lseek (stream,0,2);
#ifdef PRECISE
    int j;
    for (j = 0 ; j < pcount ; j++)
    {
        position_counter = 0;
#endif
    fseek (stream,position_counter,0);
    sprintf (to_search, "<%s ",tree);
    to_search_len = strlen(to_search);
    for (i = 0 ; i < to_search_len ; i++) read[i] = fgetc(stream);
    read[i] = 0;
    while (strcmp(read,to_search) && position_counter < fsize)
    {
        fseek(stream,++position_counter,0);
        for (i = 0 ; i < to_search_len ; i++) read[i] = fgetc(stream);
        read[i] = 0;
    }
    if (!strcmp(read,to_search))
    {
        int tree_start = position_counter;
        fseek (stream,++position_counter,0);
        char c = fgetc(stream);
        char d = fgetc(stream);
        while (c != '/' && d != '>' && position_counter < fsize)
        {
            fseek (stream,++position_counter,0);
            c = fgetc(stream);
            d = fgetc(stream);
        }
        if (c == '/' && d == '>')
        {
            int tree_len = position_counter + 1 - tree_start;
            fseek (stream,tree_start,0);
            for (i = 0 ; i <= tree_len ; i++) tmp[i] = fgetc(stream);
            memset (read,0,128);
            char tmp2[32] = {0,};
#ifdef FAST
            int j;
            for (j = 0 ; j < pcount ; j++)
            {
#endif
                sprintf (tmp2,"%s=\"", params[j]);
                int tmp2_len = strlen (tmp2);
                for (i = 0 ; i < tmp2_len ; i++) dest[j][i] = tmp[i+to_search_len];
                dest[j][i] = 0;
                while (strcmp(dest[j],tmp2))
                {
                    ++to_search_len;
                    for (i = 0 ; i < tmp2_len ; i++) dest[j][i] = tmp[i+to_search_len];
                    dest[j][i] = 0;
                }
                if (!strcmp(dest[j],tmp2))
                {
                    int param_len = strlen(params[j]);
                    memset (dest[j],0,32);
                    for (i = 0 ; i < 10 ; i++)
                    {
                        char c = tmp[to_search_len+param_len+i+2];
                        if (c != 0x20 && c != '<' && c != '"')
                        {
                            dest[j][i] = c;
                        }
                    }
                    result = 1;
                }
            }
        }
    }
    return result;
}

int potega(int liczba , int potega )
{
    int wynik = liczba;
    for(int i=1;i<potega;i++)
    {
        wynik = wynik * liczba;
    }
    return wynik;
}

int strings2int (char src[][10], int *dest, int pcount)
{
    int i;
    for (i = 0 ; i < pcount ; i++)
    {
        int len = strlen (src[i]);
        int l2 = len;
        int j;
        for (j = 0 ; j < len ; j++)
        {
            dest[i] += (src[i][j] - 0x30) * (l2 == 1 ? 1 : potega(10,l2-1));
            l2--;
        }
    }
    return 1;
}
